/*
 * Session.h
 *
 *  Created on: 2009-3-18
 *      Author: P. Weiwen
 */


#ifndef SESSION_H_
#define SESSION_H_
#include "SocketPlus/Socket.h"
#include "XPacket.h"
#include "Tunnel.h"

#define		MAX_READ_BUF	4096

class Tunnel;

class Session: public Socket {
private:
	Tunnel * m_serv_socket;		//连接server的socketv
	PacketQueue		m_up_queue;		//发送到srv队列
	PacketQueue		m_down_queue;	//发送到cli队列
	u_short			m_sn;
	static u_short  m_count;
//	int Read(XPacket &xp, EventArg &e);

public:
	Session();

	//获取会话编号
	u_short GetNo() const{
		return m_sn;
	}
	virtual int OnRecvCli(XPacket &xp);		//从会话对端接收数据
	virtual int OnRecvSrv(XPacket &xp);		//从会话关联接收数据

	int SendToCli(XPacket& xp);
	int SendToSrv(XPacket& xp);

	//EpollHandler
	int OnRead(Epoller &sender, EpollEventArg & e);
	int OnWrite(Epoller &sender, EpollEventArg & e);
//	int OnError(Epoller &sender, EpollEventArg & e);
	//end EpollHandler

	int SetPeerAddr(SockAddr & addr){
		memcpy(m_peer_addr,&addr,sizeof(addr));
		return 0;
	}
	int BindTunnel(Tunnel * tunnel){
		m_serv_socket = tunnel;
		return 0;
	}
	int Send();
	//测试用
	PacketQueue & GetQueue(){
		if(m_up_queue.empty()){
			debug("empty!\n");
		}else{
			debug("ok\n");
		}
		return m_up_queue;
	}
	virtual ~Session();
};


#endif /* SESSION_H_ */
